# -*- coding: utf-8 -*-
"""
@Time    : 2024/6/25 16:27 
@Author  : ZhangShenao 
@File    : basic.py 
@Desc    : 异常处理机制
"""


def print_fruit(fruits):
    """
    使用try-except机制处理异常
    可以使用不同的except block来捕获不同的异常
    """
    try:
        print(f'fruits[2] is :{fruits[2]}')
    except IndexError as err:
        print(f'出现了索引越界异常: {err}')
    except Exception as err:
        print(f'出现了其他异常: {err}')


def try_except_finally():
    """
    使用try-except-finally机制处理异常
    """
    try:
        print('执行业务逻辑')
        # print([1, 2, 3][4])
        # print(1 / 0)
    except IndexError as ie:
        print(f'出现了索引越界异常: {ie}')
    except Exception as err:
        print(f'出现了其他异常: {err}')
    finally:
        print('执行finally语句,实现资源的释放')


if __name__ == '__main__':
    # fruits = ['apple', 'banana', 'cherry']
    # print_fruit(fruits)
    # fruits = ['apple', 'banana']
    # print_fruit(fruits)

    try_except_finally()
